﻿using cnBlogs.Common;
using cnBlogs.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using AllanXingCommonLibrary;
using cnBlogLibrary;

namespace cnBlogs.ViewModel
{
    class GetSubCategoryData : GetFeedsBase
    {
        Dictionary<string, ObservableCollection<ArticleCategoryInfo>> _dictionartySubCategory = new Dictionary<string, ObservableCollection<ArticleCategoryInfo>>();

        public Dictionary<string, ObservableCollection<ArticleCategoryInfo>> DictionartySubCategory
        {
            get { return _dictionartySubCategory; }
            set { _dictionartySubCategory = value; }
        }
        public GetSubCategoryData()
        {
            this.PostContent_String = "{\"cateIds\":\"108698,2,108701,108703,108704,108705,108709,108712,108724,4\"}";
        }

        protected override void Parse(string feed)
        {
            MatchCollection submatches = Regex.Matches(feed, "<div id=\"cate_content_block_(?<ID>.*?)\" onmouseover=\"cateShow\\u0028(?<ID>.*?)\\u0029\" onmouseout=\"cateHidden\\u0028(?<ID>.*?)\\u0029\" class=\"cate_content_block_wrapper\" .*?>.*?</div></div>");
            foreach (Match item in submatches)
            {
                string id = item.Groups["ID"].Value;
                ObservableCollection<ArticleCategoryInfo> sublist = new ObservableCollection<ArticleCategoryInfo>();
                _dictionartySubCategory[id] = sublist;
                MatchCollection subCategoryItem = Regex.Matches(item.Value, "<a href=\"(?<link>.*?)\">(?<Name>.*?)\\u0028(?<newcount>.*?)\\u0029</a>");
                foreach (Match subitem in subCategoryItem)
                {
                    ArticleCategoryInfo sub = new ArticleCategoryInfo();
                    sub.CategoryID = id;
                    sub.CategoryName = subitem.Groups["Name"].Value;
                    sub.DailyNewArticleCount = Convert.ToUInt32(subitem.Groups["newcount"].Value);
                    sub.CategoryLink = subitem.Groups["link"].Value;
                    sublist.Add(sub);
                }

            }



        }

        protected override string GetUrl()
        {
            return AppResource.cnBlogsUlr + "aggsite/SubCategories";
        }
    }
}
